#include <QtGui>
#include "emboard.h"
#include "board.h"
#include "lcd.h"

MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
    : QMainWindow(parent, flags)
{
    //QPushButton *startButton = new QPushButton("Start");
    QPushButton *runButton = new QPushButton("Run");
    QPushButton *stepButton= new QPushButton("Step");
    QPushButton *stopButton= new QPushButton("Stop");
    QPushButton *dumpButton= new QPushButton("DumpRegs");
    QPushButton *key1Button= new QPushButton("Key1");
    QVBoxLayout *layout = new QVBoxLayout();
    //layout->addWidget(startButton);
    layout->addWidget(stopButton);
    layout->addWidget(stepButton);
    layout->addWidget(runButton);
    layout->addWidget(dumpButton);
    layout->addWidget(key1Button);
    //connect(startButton, SIGNAL(clicked()), this, SLOT(startClicked()));
    connect(stopButton, SIGNAL(clicked()), this, SLOT(stopClicked()));
    connect(stepButton, SIGNAL(clicked()), this, SLOT(stepClicked()));
    connect(runButton, SIGNAL(clicked()), this, SLOT(runClicked()));
    connect(dumpButton, SIGNAL(clicked()), this, SLOT(dumpClicked()));
    connect(key1Button, SIGNAL(pressed()), this, SLOT(key1Pressed()));
    connect(key1Button, SIGNAL(released()), this, SLOT(key1Released()));

    QWidget* mainWidget = new QWidget(this);
    mainWidget->setLayout(layout);

    setCentralWidget(mainWidget);

    mLcd = new LcdDialog();
    mLcd->setFixedSize(240, 320);
    mLcd->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
    mLcd->setWindowTitle("LCD (240x320)");

    QTimer::singleShot(2000, this, SLOT(fillImage()));
}

MainWindow::~MainWindow()
{
}

void MainWindow::fillImage()
{
    QImage *img = new QImage(240, 320, QImage::Format_RGB888);
    img->fill(0);
    mLcd->setImage(img);
}

/*
void MainWindow::startClicked()
{
    em_start();
}
*/

void MainWindow::stopClicked()
{
    em_stop();
}

void MainWindow::stepClicked()
{
    em_step();
}

void MainWindow::runClicked()
{
    em_run();
}

void MainWindow::dumpClicked()
{
    em_dump_regs();
}

void MainWindow::key1Pressed()
{
    em_sendevent("key1-pressed");
}

void MainWindow::key1Released()
{
    em_sendevent("key1-released");
}

void MainWindow::showLcd()
{
    mLcd->show();
    mLcd->startSync();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    mLcd->close();
    event->accept();
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    em_init();
    em_start();

    MainWindow *win = new MainWindow();

    win->setWindowTitle("ETB Simulator");
    win->showLcd();
    win->show();

    app.exec();

    return 0;
}
